/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gameloft.glf;

import java.io.*;
import android.os.Environment;
import android.app.Activity;
import android.content.res.AssetManager;

// Wrapper for native library
public class GL2JNILib {
	static String s_resourcePath;
	
	/*static {
		try {
			String path = GL2JNILib.class.getResource("/AndroidManifest.xml").getPath();
			if(path.indexOf('!') > 0)
				path = path.substring(0, path.indexOf('!'));
			setPackagePath(path);
		} catch(Error e) {
			e.printStackTrace();
		}
	}*/
	
	private static void ensurePathExists(String path) {
		try {
			new File(path).mkdirs();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void setupPaths() {
		File externDir = Environment.getExternalStorageDirectory();
		String resourcePath = s_resourcePath;
		if (resourcePath == null) {
			resourcePath = 
				externDir.getAbsolutePath() + 
				"/gameloft/games/" + 
				GL2JNIActivity.s_activity.getLocalClassName();
		}
		String homePath = GL2JNIActivity.s_activity.getFilesDir().getAbsolutePath();
		String tempPath = GL2JNIActivity.s_activity.getCacheDir().getAbsolutePath();

		ensurePathExists(resourcePath);
		ensurePathExists(homePath);
		ensurePathExists(tempPath);
		
		//File f = new File(resourcePath + "/icon.png");
		//boolean exist s = f.exists();
		
		setPaths(resourcePath, homePath, tempPath);
	}
	
	public static void setResourcePath(String path) {
		s_resourcePath = path;
	}
	
	public static void createView() {
		GL2JNIActivity.s_activity.m_view = new GL2JNIView(GL2JNIActivity.s_activity.getApplication(), false);
		GL2JNIActivity.s_activity.setContentView(GL2JNIActivity.s_activity.m_view);
	}
	
	public static int mPixelSize = 16;
	public static int mDepthSize = 16;
	public static int mStencilSize = 0;
	public static int mCSAA = 0;
	public static int mDepthNonLinear = 0;
	
	public static void setViewSettings(int pixelSize, int depthSize, int stencilSize, int csaa, int depthNonLinear) {
		mPixelSize = pixelSize;
		mDepthSize = depthSize;
		mStencilSize = stencilSize;
		mCSAA = csaa; 
		mDepthNonLinear = depthNonLinear;
	}

	public static byte[] getResource(String path) {
		try {
			if(path.startsWith("."))
				path = path.substring(1);
			
			//DataInputStream is = new DataInputStream(s_activity.getAssets().open(path, AssetManager.ACCESS_STREAMING));
			DataInputStream is = new DataInputStream(GL2JNILib.class.getResourceAsStream(path));
			if(is != null) {
				System.out.print("getResource " + path + ": " + is.available() + " bytes");
				byte[] buffer = new byte[is.available()];
				is.readFully(buffer);
				is.close();
				return buffer;
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean setCurrentContext(int id) {
		return GL2JNIActivity.s_activity.m_view.setCurrentContext(id);
	}
	
	public static void enableAccelerometer(boolean enabled, float frequency) {
		GL2JNIActivity.s_activity.enableAccelerometer(enabled, frequency);
	}
    
	public static native void init();
	public static native void initGL();
	public static native void setPaths(String resourcePath, String homePath, String tempPath);
	public static native void stateChanged(boolean active);
	public static native void getViewSettings();
	public static native void resize(int width, int height);
	public static native void destroy();
	public static native void step();
	public static native void touchEvent(int flag, int x, int y , int id);
	public static native void accelerometerEvent(float ax, float ay, float az);
	public static native void orientationChanged(int angle);

	public static native int getNumExtraContext();
	public static native void setNumExtraContext(int n);
}
